package com.mvc.service.impl;

import com.jfinal.kit.StrKit;
import com.mvc.model.Deeds;
import com.mvc.model.User;
import com.mvc.request.ReqStory;
import com.mvc.request.ReqStoryEdit;
import com.mvc.request.ReturnData;
import com.mvc.service.StoryService;

/***
 * 样式风格
 * 
 * @author Swer
 * 
 */
public class StoryServiceImpl implements StoryService {

	/***
	 * 根据场馆ID获取场馆生平事迹
	 */
	public ReturnData<String> querStory(ReqStory req) {

		if (req == null) {
			return new ReturnData<String>("", 500, "请求参数不完整!");
		}
		if (req.getVenueId() <= 0) {
			return new ReturnData<String>("", 202, "请求参数不完整");
		}
		
		Deeds deeds = Deeds.dao.getDeeds(req.getVenueId());
		if (deeds == null) {
			return new ReturnData<String>("", 202, "无内容");
		}
		 return new ReturnData<String>(deeds.getStr(Deeds.TableField.CONTENT), 200, "OK");
	}

	/***
	 * 根据场馆ID编辑场馆生平事迹
	 * 
	 * @return
	 */
	public ReturnData<Boolean> editStory(ReqStoryEdit req) {
		

		if (req == null) {
			return new ReturnData<Boolean>(false, 500, "请求参数不完整!");
		}
		if (req.getVenueId() <= 0 || req.getUserId() <=0) {
			return new ReturnData<Boolean>(false, 202, "请求参数不完整");
		}
		if (StrKit.isBlank(req.getContent())) {
			return new ReturnData<Boolean>(false, 202, "请输入生平事迹");
		}
		
		User user = User.dao.findById(req.getUserId());
		if (user ==null)
		{
			return new ReturnData<Boolean>(false, 202, "当前用户不存在，请重新登录!");
		}
		
		// 3 判断用户mac 与 数据库mac是否相同返回都是 错误400
		if ( !req.getFinalData().getMac().equals(user.getStr("mac"))) {
			return new ReturnData<Boolean>(false, 400, "当前用户已在其他设备登录，请重新登录!");
		}

		
		Deeds deeds = Deeds.dao.getDeeds(req.getVenueId());
		boolean success =true;
		if (deeds == null) {
			deeds = new Deeds();
			deeds.set(Deeds.TableField.VENUEID, req.getVenueId());
			deeds.set(Deeds.TableField.CONTENT, req.getContent());
			success = deeds.save();
		}else {
			deeds.set(Deeds.TableField.CONTENT, req.getContent());
			success = deeds.update();
		}
		 return new ReturnData<Boolean>(success, success?200:201, "OK");
		 
	}

}
